﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Dapper.Contrib.Extensions;
using hello.csharp.common.Enums;
using hello.csharp.common.Interfaces;
using hello.csharp.common.Interfaces.Storages;
using hello.csharp.domain;
using hello.csharp.domain.Base;

namespace hello.csharp.storage
{
    public class UserSnackStorageRead : IUserSnackStorageRead
    {
        private readonly IDbManager _dbManager;

        public UserSnackStorageRead(IDbManager dbManager)
        {
            _dbManager = dbManager;
        }

        /// <summary>
        /// 当前实现面向的数据表表名
        /// </summary>
        public string TableName { get; } = $"{nameof(UserSnack)}s";

        /// <summary>
        /// 分页查询方法
        /// </summary>
        /// <param name="pageNumber">当前页码。从0开始。</param>
        /// <param name="pageSize">每页的数据数量。</param>
        /// <param name="direction">查询数据时的排序方向。</param>
        /// <param name="skipDeleteTag">是否跳过打上删除标记的记录，默认(当false时)有删除标记的记录不包含在结果中</param>
        /// <returns>当前页的数据集合</returns>
        public async Task<IEnumerable<UserSnack>> PageAsync(uint pageNumber, uint pageSize, SortDirection direction, bool skipDeleteTag = false)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 根据指定的ID获取指定的记录并转换为对象
        /// </summary>
        /// <param name="id">指定的ID</param>
        /// <param name="skipDeleteTag">是否跳过打上删除标记的记录，默认(当false时)有删除标记的记录不包含在结果中</param>
        /// <returns></returns>
        public async Task<UserSnack> FindOneByIdAsync(string id, bool skipDeleteTag = false)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 指定ID的记录是否存在
        /// </summary>
        /// <param name="id">指定的记录ID</param>
        /// <param name="skipDeleteTag">是否跳过打上删除标记的记录，默认(当false时)有删除标记的记录不包含在结果中</param>
        /// <returns>记录是否存在，true时存在指定ID的记录，false反之。</returns>
        public async Task<bool> ExistAsync(string id, bool skipDeleteTag = false)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 查询记录的数据记录统计数量
        /// </summary>
        /// <param name="skipDeleteTag">是否跳过打上删除标记的记录，默认(当false时)有删除标记的记录不包含在结果中</param>
        /// <returns>数量</returns>
        public async Task<uint> CountAsync(bool skipDeleteTag = false)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 值是否唯一
        /// </summary>
        /// <param name="onlyItem">唯一项类型</param>
        /// <param name="value">值</param>
        /// <returns>true已存在，false不存在</returns>
        public async Task<bool> ExistByOnlyItemAsync(OnlyItem onlyItem, string value)
        {
            var conn = _dbManager.OpenReadConnection();
            var where = string.Empty;
            switch (onlyItem)
            {
                case OnlyItem.Email:
                    where = $"{nameof(UserSnack.NormalizedEmail)}='{value}'";
                    break;
                case OnlyItem.Nickname:
                    where = $"{nameof(UserSnack.NormalizedNickname)}='{value}'";
                    break;
                case OnlyItem.Mobile:
                    where = $"{nameof(UserSnack.Mobile)}='{value}'";
                    break;
            }

            var sql = $"SELECT * FROM {TableName} WHERE {where}";// AND State='{(short)RecordState.Normal}'";
            var us = await conn.QuerySingleAsync<UserSnack>(sql);
            return us != null;
        }
    }
}
